﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using FPP.Models;
using FPP.Custom;
using FPP.Utilities;


namespace FPP.Controllers
{
    public class NewsController : Controller
    {
        const int OutputCache = 600;
        readonly fppEntities _context = new fppEntities(DataAccess.ffpCString);

        //
        // GET: /News/
        [OutputCache(Duration = OutputCache)]
        public ActionResult Index()
        {
            var lNews = new List<News>();

            Services.CheckServicesAsync();

            using (_context)
            {
                var q = (from itm in _context.News
                         orderby itm.DateOfNews descending
                         select itm).Take(10);

                lNews.AddRange(q);
            }

            return View(lNews);
        }

        //
        // GET: /News/Details/5

        public ActionResult Details(int id)
        {
            News news;

            using (_context)
            {
                news = _context.News.Single(q => q.Id == id);
            }
            return View(news);
        }

        //
        // GET: /News/Create
        [CustomAuthorize]
        public ActionResult Create()
        {
            var nNews = new News();
            return View(nNews);
        }

        //
        // POST: /News/Create
        [CustomAuthorize]
        [HttpPost]
        public ActionResult Create(News nNews)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    using (_context)
                    {
                        _context.News.AddObject(nNews);
                        _context.SaveChanges();

                        return RedirectToAction("Index");
                    }
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /News/Edit/5
        [CustomAuthorize]
        public ActionResult Edit(int id)
        {
            News nNews;

            using (_context)
            {
                nNews = _context.News.Single(q => q.Id == id);
            }

            return View(nNews);
        }

        //
        // POST: /News/Edit/5
        [CustomAuthorize]
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                if (ModelState.IsValid)
                {

                    News news = _context.News.Single(q => q.Id == id);

                    if (TryUpdateModel(news))
                    {
                        _context.SaveChanges();
                        RedirectToAction("Details", new { id });
                    }

                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
